package cn.edu.xmu.oomall.customer.dao;

import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.oomall.customer.controller.dto.UserQueryDto;
import cn.edu.xmu.oomall.customer.dao.bo.Customer;
import cn.edu.xmu.oomall.customer.mapper.jpa.CustomerPoMapper;
import cn.edu.xmu.oomall.customer.mapper.po.CustomerPo;
import lombok.RequiredArgsConstructor;
import cn.edu.xmu.javaee.core.util.CloneFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author 谢传艳
 * @date 2024/12/18
 */
@Repository
@RequiredArgsConstructor
public class CustomerDao {
    private final CustomerPoMapper customerPoMapper;


    private void build(Customer bo){
        bo.setCustomerDao(this);
    }


    private Customer build(CustomerPo po){
        Customer bo = CloneFactory.copy(new Customer(), po);
        this.build(bo);
        return bo;
    }

    /**
     * 通过用户名获取用户
     * @param userName 用户名
     * @return Customer
     */
    public Customer retrieveByUserName(String userName) {
        Optional<CustomerPo> customerPo = customerPoMapper.findByUserName(userName);
        if(customerPo.isPresent()) {
            return CloneFactory.copy(new Customer(), customerPo.get());
        }else {
            return null;
        }
    }

    /**
     * 通过手机号获取用户
     * @param mobile 手机号
     * @return Customer
     */
    public Customer retrieveByMobile(String mobile) {
        Optional<CustomerPo> customerPo = customerPoMapper.findByMobile(mobile);
        if(customerPo.isPresent()) {
            return CloneFactory.copy(new Customer(), customerPo.get());
        }else {
            return null;
        }
    }

    /**
     * 插入用户
     * @param customer 用户信息
     * @return Customer
     */
    public Customer insert(Customer customer) {
        CustomerPo customerPo = CloneFactory.copy(new CustomerPo(), customer);
        customerPoMapper.save(customerPo);
        customer.setId(customerPo.getId());
        return customer;
    }

    /**
     * 通过id获取用户
     * @param id 用户id
     * @return Customer
     */
    public Customer findById(Long id) {
        Optional<CustomerPo> po = customerPoMapper.findById(id);
        if(po.isPresent()) {
            return build(po.get());
        }else {
            throw new BusinessException(ReturnNo.CUSTOMERID_NOTEXIST);
        }
    }

    /**
     * 更新用户信息
     * @param customer 用户信息
     */
    public void save(Customer customer) {
        customerPoMapper.save(CloneFactory.copy(new CustomerPo(), customer));
    }

    /**
     * 通过用户名和姓名和手机号获取用户
     * @param userQueryDto 用户查询信息
     * @param page 页码
     * @param pageSize 每页大小
     * @return List<Customer>
     */
    public List<Customer> retrieveByUserNameAndNameAndMobile(UserQueryDto userQueryDto, Integer page, Integer pageSize) {
        Pageable pageable = PageRequest.of(page - 1, pageSize);
        Page<CustomerPo> customerPoPage = customerPoMapper.findByUserNameAndNameAndMobile(userQueryDto.getUsername(), userQueryDto.getName(), userQueryDto.getMobile(), pageable);
        if(customerPoPage.isEmpty()) {
            return new ArrayList<>();
        }else{
            return customerPoPage.get().map(po -> CloneFactory.copy(new Customer(), po)).toList();
        }
    }

    /**
     * 删除用户
     * @param customer 用户信息
     */
    public void delete(Customer customer) {
        CustomerPo customerPo = CloneFactory.copy(new CustomerPo(), customer);
        customerPo.setBeDeleted((byte) 1);
        customerPoMapper.save(customerPo);
    }

}
